package com.jeason.fileserver.config;

import com.jeason.fileserver.domain.LoginUser;
import com.jeason.fileserver.utils.StringUtils;
import com.jeason.fileserver.utils.TokenService;
import com.jeason.fileserver.utils.ip.IpUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author: jeason
 * @Description:
 * @Date: 2021/1/18 21:48
 */
@Component
@Slf4j
public class MyInterceptor implements HandlerInterceptor {
    @Autowired
    private TokenService tokenService;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        LoginUser loginUser = tokenService.getLoginUser(request);
        if (StringUtils.isNotNull(loginUser))
        {
            tokenService.verifyToken(loginUser);
            return true;
        }
        StringBuffer requestURL = request.getRequestURL();
        log.error("请重新登录,再访问:"+requestURL.toString());
        //表示该用户没有登录，输出 出错信息
        throw new RuntimeException("请重新登录,再访问:"+requestURL.toString());
    }

}
